import { observable, action } from 'mobx';
import _ from 'lodash';
import { WeaLocaleProvider } from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

export interface TopStoreProps {
    icon: string;
    name: string;
    total: number;
    iconBgcolor: string;
    iconColor: string;
    extra: string;
    tabs: any[];
    groupCount: any;
    selectedKey: string;
    isInit: boolean;
    update: Function;
    title: string;
    hasTabKeyNamedEmpty: boolean;
    onHandleEmptyTab: Function;
}

export default class TopStore implements TopStoreProps {
    @observable icon = 'icon-coms-ModelingEngine';
    @observable name = '';
    @observable total = 0;
    @observable iconBgcolor = "#96358a";
    @observable extra = '';
    @observable tabs = [] as {title?: string, name?: string, key: string, showcount?: boolean}[];
    @observable groupCount = {};
    @observable selectedKey = "all";
    @observable isInit = false;
    @observable title = '';
    @observable iconColor = '';
    @observable hasTabKeyNamedEmpty: boolean = true;

    @action
    update = (param) => {
        for (let key in param) {
            if (key == 'tabs') {
                this.tabs = param[key].map(t => ({ ...t, showcount: true, }));
            } else {
                this[key] = param[key]
            }
        }
    }

    @action
    onHandleEmptyTab = (emptyGroupCount) => {  //未分组为0，则不显示
        if (!this.hasTabKeyNamedEmpty && emptyGroupCount) {
            this.tabs.push({ key: 'empty', title: getLabel('81307', '未分组'), showcount: true });
            this.hasTabKeyNamedEmpty = true;
        } else {
            if (emptyGroupCount == undefined || emptyGroupCount == 0) {
                const that = this;
                _.remove(this.tabs, function (tab: object) {
                    if (tab['key'] == 'empty') {
                        that.hasTabKeyNamedEmpty = false;
                        return true;
                    }
                });
            }
        }
    }
}
